#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
示例文件：展示如何使用UI自动化测试用例元数据类
"""

from datetime import datetime
import sys
import os

# 添加项目根目录到Python路径
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from modules.project.models import UiTestCase, TestStep


def create_sample_test_case():
    """创建一个示例UI测试用例"""
    # 创建测试用例
    test_case = UiTestCase(
        name="登录功能测试",
        owner="测试工程师",
        category="功能测试",
        description="验证用户能够使用正确的用户名和密码登录系统",
        priority="高"
    )
    
    # 设置前置条件
    test_case.prerequisites = "应用已安装且网络连接正常"
    
    # 添加标签
    test_case.tags = ["登录", "认证", "UI测试"]
    
    # 设置设备要求
    test_case.device_requirements = {
        "os": "OpenHarmony",
        "version": ">=3.0",
        "screen_size": ">=5.0英寸"
    }
    
    # 添加测试步骤
    # 步骤1：启动应用
    step1 = TestStep(
        action="启动应用",
        target="com.example.app",
        expected_result="应用成功启动并显示登录界面"
    )
    test_case.add_step(step1)
    
    # 步骤2：输入用户名
    step2 = TestStep(
        action="输入",
        target="id:username_input",
        params={"text": "testuser"},
        expected_result="用户名输入框中显示输入的文本"
    )
    test_case.add_step(step2)
    
    # 步骤3：输入密码
    step3 = TestStep(
        action="输入",
        target="id:password_input",
        params={"text": "password123"},
        expected_result="密码输入框中显示掩码文本"
    )
    test_case.add_step(step3)
    
    # 步骤4：点击登录按钮
    step4 = TestStep(
        action="点击",
        target="id:login_button",
        expected_result="系统验证登录信息并跳转到主界面"
    )
    test_case.add_step(step4)
    
    # 步骤5：验证登录成功
    step5 = TestStep(
        action="检查元素",
        target="id:welcome_message",
        params={"text_contains": "欢迎"},
        expected_result="主界面显示欢迎信息，表明登录成功"
    )
    test_case.add_step(step5)
    
    # 更新测试用例状态
    test_case.update_status("待执行")
    
    # 模拟添加执行记录
    test_case.add_execution_record(
        result="通过",
        device_id="device_001",
        execution_time=datetime.now(),
        duration=5.2,
        log_path="/logs/test_001.log"
    )
    
    return test_case


def print_test_case_info(test_case):
    """打印测试用例信息"""
    print(f"\n{'='*50}")
    print(f"测试用例: {test_case.name}")
    print(f"{'='*50}")
    print(f"描述: {test_case.description}")
    print(f"所有者: {test_case.owner}")
    print(f"类别: {test_case.category}")
    print(f"优先级: {test_case.priority}")
    print(f"状态: {test_case.status}")
    print(f"创建时间: {test_case.first_edit}")
    print(f"最后修改时间: {test_case.last_edit}")
    print(f"前置条件: {test_case.prerequisites}")
    print(f"标签: {', '.join(test_case.tags)}")
    print(f"设备要求: {test_case.device_requirements}")
    
    print("\n测试步骤:")
    for i, step in enumerate(test_case.steps, 1):
        print(f"\n步骤 {i}:")
        print(f"  操作: {step.action}")
        print(f"  目标: {step.target}")
        if step.params:
            print(f"  参数: {step.params}")
        print(f"  预期结果: {step.expected_result}")
    
    print("\n执行历史:")
    for i, record in enumerate(test_case.execution_history, 1):
        print(f"\n记录 {i}:")
        print(f"  结果: {record['result']}")
        print(f"  设备ID: {record['device_id']}")
        print(f"  执行时间: {record['execution_time']}")
        print(f"  执行时长: {record['duration']}秒")
        if record['log_path']:
            print(f"  日志路径: {record['log_path']}")


if __name__ == "__main__":
    # 创建示例测试用例
    sample_test = create_sample_test_case()
    
    # 打印测试用例信息
    print_test_case_info(sample_test)
    
    print("\n示例完成。这个文件展示了如何创建和使用UI自动化测试用例元数据类。")